package com.mrli.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.websocket.server.ServerEndpointConfig;

/**
 * description
 *
 * @author LiDaShuai
 * @version 1.0
 * @project mrli-mall-auto-issu
 * @date: 2023/2/8 14:08
 * @copyright 2009–2022xxxxx all rights reserved.
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig  extends ServerEndpointConfig.Configurator implements WebSocketConfigurer {
    private final MessageHandler messageHandler;

    private final MessageHandShakeInterceptor messageHandShakeInterceptor;

    public WebSocketConfig(MessageHandler messageHandler, MessageHandShakeInterceptor messageHandShakeInterceptor) {
        this.messageHandler = messageHandler;
        this.messageHandShakeInterceptor = messageHandShakeInterceptor;
    }

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }


    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(messageHandler,"websocket/{userId}").setAllowedOrigins("*").addInterceptors(messageHandShakeInterceptor);
    }
}
